//
//  DataManager.m
//  QuicklyPlay
//
//  Created by lanou3g on 15/11/10.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "DataManager.h"


static DataManager *db;
@interface DataManager ()
@property (nonatomic ,strong)FMDatabase *data;
@property(nonatomic,copy)NSString *recordStr;
@end

@implementation DataManager
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        db= [DataManager new];
        
        [db createDateBase];
    });
    return db;
}
- (NSMutableArray *)dataArray {
    if (_dataArray==nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}












- (void)createDateBase{
    
    db.data = [FMDatabase databaseWithPath:SelectedSortFileDataBase];
    
    NSLog(@"%@",SelectedSortFileDataBase);

    if ([db.data open] == NO) {
        NSLog(@"打开成功");
    }
    
    BOOL isCreate =  [db.data executeUpdate:@"CREATE TABLE CollectionList1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, vid TEXT ,title TEXT, imageUrl TEXT, m3u8 TEXT, webUrl TEXT)"];
    if (isCreate) {
        NSLog(@"创建表格成功");
        }
   
}

-(NSString *)saveDataWithDicti:(VideosModel*)model {
    NSString *messgae = [[NSString alloc] init];
    if ([self queryDataWithId:model.vid]) {
        NSLog(@"已经收藏过了");
        messgae = @"收藏过了哦";
        return messgae;
    } else {
        
   BOOL isSave = [self.data executeUpdate:@"insert into CollectionList1 (vid,title,imageUrl,m3u8,webUrl) values (?,?,?,?,?)",model.vid,model.title,model.img_url,model.m3u8_url,model.web_url];
    if (isSave) {
        NSLog(@"保存成功");
        messgae = @"保存成功";
        [self.dataArray addObject:model];
    }
        return messgae;
    }

    
}

- (BOOL)queryDataWithId:(NSString *)vid {
   FMResultSet *set =  [self.data executeQuery:@"select ID from CollectionList1 where vid = ?",vid];
    while ([set next]) {
        return YES;
    }
    return NO;
    
}

- (NSMutableArray *)getData{
    FMResultSet *set =  [self.data executeQuery:@"select imageUrl,title,m3u8,webUrl from CollectionList1"];
    [self.dataArray removeAllObjects];
    while ([set next]) {
        NSString *imageURL = [set stringForColumn:@"imageUrl"];
        NSString *title = [set stringForColumn:@"title"];
        NSString *m3u8 = [set stringForColumn:@"m3u8"];
        NSString *webUrl = [set stringForColumn:@"webUrl"];

        
        VideosModel *model = [[VideosModel alloc] init];
        model.img_url = imageURL;
        model.title = title;
        model.m3u8_url = m3u8;
        model.web_url = webUrl;
         [self.dataArray addObject:model];
        NSLog(@"%ld",(unsigned long)self.dataArray.count);
    }
    return self.dataArray;
}

- (void)deleteDataWithIndex:(NSInteger )index{
    VideosModel *model = self.dataArray[index];
  BOOL isFind =  [self.data executeUpdate:@"DELETE FROM CollectionList1 WHERE imageUrl=?",model.img_url];
    if (isFind == YES) {
        NSLog(@"删除成功");
        [self.dataArray removeObject:model];
    }else{
        NSLog(@"删除失败");
    }
    
}

@end
